<?php

class IndexController extends Site_Controller_Action
{
    private $_table     = 'PAGINA';
    private $_pk        = 'PAG_ID';
    private $_order_col = 'PAG_ORD';
    private $_order     = 'ASC';
    private $_sta_col   = 'PAG_STA';
    private $_file_col  = 'PAG_FILE'; 
    public  $dir = 'public/upload/pagina/';
    public  $db = '';
    
    public function init()
    {
        
    }

    public function indexAction()
    {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/tela.js?1='.rand(1, 99999));
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/index.js?1='.rand(1, 99999));
       
        $where = $this->_pk . ' > 0 AND PAG_TIP	in(1,2) AND PAG_STA = 1';
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $result;
    }
    
    public function paginaAction()
    {        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');

        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/pagina.js?1='.rand(1, 99999));
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/swipebox-master/src/js/jquery.swipebox.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/swipebox-master/src/css/swipebox.css');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/tela.js?1='.rand(1, 99999));
        
        $where = $this->_pk . ' > 0  AND PAG_STA = 1';
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $result;
        
        $id = $this->_getParam('id', 0);
        $where = $this->_pk . ' = ' . $id.' AND PAG_STA = 1';
        if($id == 0) $where = $this->_pk . ' > 0  AND PAG_STA = 1';
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where;
        $result = $this->db->fetchRow($query);
        $this->view->row = $result;
        
        $this->view->id = $id;
    }
    
    public function contatoAction()
    {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = false;
        
        if($this->getRequest()->isPost()) {
            try {
                $this->db->beginTransaction();
                
                $formData = $this->getRequest()->getPost();
                
                $data = array(
                    'INT_NOM' => $formData['name'],
                    'INT_TEL' => $formData['telefone'],
                    'INT_EML' => $formData['email'],
                    'INT_CID' => $formData['cidade'],
                    'INT_DAT' => date('Y-m-d'),
                );
                
                $ok = (bool) $this->db->insert('INTERESSE', $data);
                
                $this->db->commit();
                
            } catch (Exception $exc) {
                //echo 'error: '.$exc->getMessage(); 
                $this->db->rollBack();
            }
        }
        
        echo json_encode(array('ok' => $ok));
        
    }
    
}

